package com.future.bantang.app;

import android.app.Application;
import android.graphics.Bitmap;
import android.os.Environment;
import android.text.TextUtils;

import com.future.bantang.R;
import com.lidroid.xutils.HttpUtils;
import com.nostra13.universalimageloader.cache.disc.DiskCache;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.memory.MemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.io.File;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by Administrator on 16-1-11.
 */
public class BanTangAPP extends Application{

    private static BanTangAPP app;

    private HttpUtils mHttpUtils;
    private ImageLoader mImageLoader;
    private OnekeyShare mOneKeyShare;

    @Override
    public void onCreate() {
        super.onCreate();
        init();

    }

    private void init() {
        app=this;
        initHttpUtils();
        initImageLoader();
        initShareSDK();
    }

    private void initShareSDK() {
        ShareSDK.initSDK(this);
        mOneKeyShare=new OnekeyShare();
    }

    private void initHttpUtils() {
        mHttpUtils=new HttpUtils();
        mHttpUtils.configSoTimeout(30*1000)
                .configTimeout(30*1000)
                .configRequestThreadPoolSize(5)
                .configRequestRetryCount(3)
                .configHttpCacheSize(20*1024*1024)
                .configDefaultHttpCacheExpiry(120*60*1000)
                .configCurrentHttpCacheExpiry(120*60*1000)
                .configResponseTextCharset("utf-8");
    }

    private void initImageLoader() {
        mImageLoader=ImageLoader.getInstance();
        int memotySize= (int) (Runtime.getRuntime().maxMemory()/8);
        MemoryCache memoryCache=new LruMemoryCache(memotySize);
        String diskCachePath=getDiskCachePath();
        DiskCache diskCache=new UnlimitedDiskCache(new File(diskCachePath));
        DisplayImageOptions options=new DisplayImageOptions
                .Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .showImageForEmptyUri(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher)
                .showImageOnLoading(R.mipmap.ic_launcher)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        ImageLoaderConfiguration configuration=new ImageLoaderConfiguration
                .Builder(this)
                .threadPoolSize(5)
                .memoryCache(memoryCache)
                .diskCache(diskCache)
                .diskCacheFileCount(200)
                .defaultDisplayImageOptions(options)
                .build();

        mImageLoader.init(configuration);
    }

    private String getDiskCachePath() {
        String path=null;
        if (TextUtils.equals(Environment.getExternalStorageState(),Environment.MEDIA_MOUNTED)
                ||!Environment.isExternalStorageRemovable()){
            path=getExternalCacheDir()+"/cache/images";
        }else{
            path=getCacheDir()+"/cache/images";
        }
        return path;
    }

    /**
     * 获取当前app实例
     * @return
     */
    public static BanTangAPP getApp(){
        return app;
    }

    /**
     * 获取httputils实例
     * @return
     */
    public HttpUtils getHttpUtils(){
        if (mHttpUtils!=null){
            return mHttpUtils;
        }
        return null;
    }

    /**
     * 获取imageloader实例
     * @return
     */
    public ImageLoader getImageLoader(){
        if (mImageLoader!=null){
            return mImageLoader;
        }
        return null;
    }


    public OnekeyShare getOneKeyShare(){
        return mOneKeyShare;
    }



}
